<?php
/*
 * Copyright (c) 2011 Todd Brady
 *
 * PS3Theatre is freely distributable under the terms of the MIT license.
 * For details, see the MIT-Liscence web site: http://www.opensource.org/licenses/mit-license.php 
 */
 
	include("includes/db_connect.php");
	$action = $_GET['action'];
	$h = $_GET['h'];
	$w = $_GET['w'];
	
	$result=mysql_query("SELECT m.title,m.directory_id,dir.http_path,m.subfolder_path,m.filename,m.tmdb_id,m.rating,m.certification,m.released_date,m.runtime,m.director,a.value as audio_value, d.value as disc_value FROM tbl_movies m LEFT OUTER JOIN tbl_audio_flags a ON m.audio_flag_id=a.id LEFT OUTER JOIN tbl_disc_flags d ON m.disc_flag_id=d.id, tbl_directories dir WHERE m.directory_id=dir.id ORDER BY title") or die(mysql_error());
	
	//variables for the top left info panel
	$leftTopLbl1 = 'Movies';
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>PS3 Theatre <?php echo $_SESSION['version'];?></title>

<script type="text/javascript" src="js/time.js"></script>
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/animator.js"></script>
<script type="text/javascript" src="js/common.js"></script>
<script type="text/javascript" src="js/ui.js"></script>
<script type="text/javascript" src="js/movies-list1080_ui.js"></script>
<script type="text/javascript" src="js/topMenuOverlay_ui.js"></script>
<script type="text/javascript">
var loaderShowing = false;
var movieListIndex = 0;
var htmlRowIndex = 0;
var htmlPageIndex = 0;

var h = <?php echo $h;?>;
var w = <?php echo $w;?>;
var movieListLength = <?php echo mysql_num_rows($result);?>;

function toggleLoader(){
	if(!loaderShowing){
		$('movieLoader').removeClassName('backgroundLoadHide');
		$('movieLoader').addClassName('backgroundLoadShow');
	}else{
		$('movieLoader').removeClassName('backgroundLoadShow');
		$('movieLoader').addClassName('backgroundLoadHide');
	}
	loaderShowing=!loaderShowing;
}

function updateMovieInfo(movieId, movieRow, certification, discFlag, audioFlag)
{
	toggleLoader();
	
	new Ajax.Request('scripts/movieImport.php?movieId='+movieId+'&medPoster=Y&uId='+getUniqueCode(),
	  {
		method:'get',
		onSuccess: function(transport){
			var json = transport.responseText.evalJSON();
			if(json.posterSrc != ''){
			  	$('moviePoster').innerHTML='<img src="'+json.posterSrc+'" height="750" width="500"/>';
				
				if(certification != ''){
					$('rating').innerHTML = '<img src="images/flags/mpaa_'+certification+'.png"/>';
				}else{
					$('rating').innerHTML = '';
				}
	
				if(discFlag != ''){
					$('discSrc').innerHTML = '<img src="images/flags/'+discFlag+'.png"/>';
				}else{
					$('discSrc').innerHTML = '';
				}
		
				if(audioFlag != ''){
					$('audioSrc').innerHTML = '<img src="images/flags/'+audioFlag+'.png"/>';
				}else{
					$('audioSrc').innerHTML = '';
				}
			}else{
				$('moviePoster').innerHTML='';
			}

			toggleLoader();
		},
		onFailure: function(){ alert('Something went wrong...');toggleLoader(); }
	  });
	 }
	 
	function showScanningWindow(action) {
		if(action != 'noScrape'){
			screenEnable(false);
			
			showConfirmWindow('Scanning', '<CENTER>Scanning for file changes...</CENTER>', 100, 300, false);
			
			new Ajax.Request('scripts/movieScraper.php?action=scrapeAll&uId='+getUniqueCode(),
			  {
				method:'get',
				onSuccess: function(transport){
					closeConfirmWindow();
					var responseText = transport.responseText.strip();
					
					if(responseText == 'no changes'){
						//do nothing, intialize screen
						initializeScreen();
						
					} else if(responseText == 'success'){
						location='movies-list1080.php?uId='+getUniqueCode()+'&w='+w+'&h='+h+'&action=noScrape';
						
					}else if(responseText.substring(10,53) == 'Fatal error</b>:  Maximum execution time of'){
						alert('Scan was shutdown after a 5 minute time limit.  It didn\'t complete and will need to be ran again to finish the rest of the scan.');
						initializeScreen();
						
					}else{
						initializeScreen();
						alert('Error scanning for changes...');
					}
				},
				onFailure: function(){ initializeScreen();alert('error scanning for changes...'); }
			  });
		  }else{
			  initializeScreen();
		  }
	}
	
	function showSortDialog(){
		showConfirmWindow('Sort Settings', '<CENTER>Loading...</CENTER>', 750, 750, false);
	}
	
	function initializeScreen(){
		selectMovieRow('movieRow0');
		var movieRow = $("movieRow0");
		updateMovieInfo(movieRow.readAttribute('tmdb_id'),'movieRow'+movieListIndex, movieRow.readAttribute('certification'), movieRow.readAttribute('disc_value'), movieRow.readAttribute('audio_value'));
		screenEnable(true);
	}
</script>
<link href="css/main.css" rel="stylesheet" type="text/css">
<meta http-equiv="Pragma" content="no-cache"> 
</head>

<body bgcolor="#B3B3B3" background="images/movies-background.png" style="overflow:hidden;" onload="clock();showScanningWindow('<?php echo $action;?>');">

<!-- top menu overlay info panel -->
<?php include('/includes/topMenuOverlay.php'); ?>

<div class="wrapper" >

<!-- left info panel -->
<?php include('/includes/topLeftInfoPanel.php'); ?>

<!-- right info panel -->
<?php include('/includes/topRightInfoPanel.php'); ?>

<!-- info box background -->
<div style="position:absolute; left:37px; top:88px; height:970px; WIDTH:1139px; overflow:auto; background:url(images/listpanel_back.png); z-index:3;"></div>

<!-- # of Movies -->
<div class="numMovesText" style="position:absolute; left:80px; top:948px; overflow:auto; z-index:4;">
<?php echo mysql_num_rows($result);?> MOVIES
</div>

<!--movie list -->
<div id="movieList" style="position:absolute; left:37px; top:85px; width:1120px; height:815px;overflow:hidden;margin-left:12px;margin-top:30px;z-index:5;">
<div id="movieScroll">
<table width=100% border=0 cellpadding="0" cellspacing="0" style="margin-bottom:-500;">
<?php
	$i = 0;
	while ($row = mysql_fetch_array($result)) {
		$link = $row['http_path'].$row['subfolder_path'].$row['filename'];
		
		//figure out css class for title length
		$titleCss = "MovieLink";
		if(strlen($row['title']) >=30 && strlen($row['title'])<35){
			$titleCss.='30';
		}else if(strlen($row['title']) >=35 && strlen($row['title'])<40){
			$titleCss.='35';
		}else if(strlen($row['title']) >=40 && strlen($row['title'])<45){
			$titleCss.='40';
		}else if(strlen($row['title']) >=45 && strlen($row['title'])<50){
			$titleCss.='45';
		}else if(strlen($row['title']) >=50){
			$titleCss.='50';
		}
?>
    <tr id="movieRow<?php echo $i;?>" class="movieUnselected" link="<?php echo $link;?>" tmdb_id="<?php echo $row['tmdb_id'];?>" certification="<?php echo $row['certification'];?>" disc_value="<?php echo $row['disc_value'];?>" audio_value="<?php echo $row['audio_value'];?>"><td height="113">
	<table width="100%" border="0" cellpadding="0" cellspacing="0">
	<tr>
	<td width="80%" id="movieRow<?php echo $i;?>Title" style="padding-left:30px;" class="<?php echo $titleCss;?>">
	<?php echo $row['title'];?></td>
	<td width="20%">
		<div id="movieRow<?php echo $i;?>Year" align="center" class="movieYear">
	<?php
		if($row['released_date'] != '0000-00-00'){
			echo ''.date("Y", strtotime($row['released_date']));
		}
		?>
		</div>
	  </td>
	</tr>
	<tr>
	<td width="80%" id="movieRow<?php echo $i;?>Dir" style="padding-left:30px;" class="movieDirector"><?php
	if($row['director'] != ''){
		echo 'DIRECTOR: '.$row['director'].' - '.$row['runtime'].' MIN';
	}
	?></td>
	<td width="20%">
	  <div align="center">
	  <?php
	  if($row['rating'] > 0){
	  	echo '<img src="images/rating'.floor($row['rating']/2).'.png" width="100" height="22" class="movieRating" />';
	  }
	  ?>
	  </div>
	  </td>
	</tr>
	</table>
	</td>
	</tr>
	<tr>
        <td height="1" style="background:url(images/listpanel_line.png) no-repeat;"></td>
	</tr>
	<?php 
		$i++;
		} 
	?>
  </table>
  </div>
</div>

<!-- scrollbar back -->
<div style="position:absolute; left:1168px; top:100px; z-index:6;">
<table border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><img src="images/scrollbarback_vertical_top.png" height="22" width="17"/></td>
  </tr>
  <tr>
    <td style="height:850px; background:url(images/scrollbarback_vertical_middle.png) repeat-y;"></td>
  </tr>
  <tr>
    <td><img src="images/scrollbarback_vertical_bottom.png" height="22" width="17"/></td>
  </tr>
</table>
</div>

<!-- scrollbar nub -->
<div id="scrollNub" style="position:absolute; left:1170px; top:119px; z-index:6;" class="scrollNub">
</div>

<!-- detail vertical bar -->
<div style="position:absolute;left:983px;top:100px;height:835px; width:148px;background:url(images/listpanel_detailwide.png);z-index:6;">
</div>

<!-- movie poster -->
<div id="moviePoster" style="position:absolute;left:1243px;top:100px;z-index:6;">
</div>

<!-- movie poster load icon -->
<div id="movieLoader" class="backgroundLoadHide" align="center" style="position:absolute; width:500px;left:1236px;top:425px;z-index:7;">
<img src="images/ajax-loader.gif"/>
</div>

<!-- disc source -->
<div id="discSrc" style="position:absolute;left:1185px;top:875px;z-index:6;">
</div>

<!-- rating -->
<div id="rating" style="position:absolute;left:1356px;top:901px;z-index:6;">
</div>

<!-- audio source -->
<div id="audioSrc" style="position:absolute;left:1615px;top:875px;z-index:6;">
</div>

<!-- background gradient effect -->
<div style="position:absolute; top:55px; height:970px; background:url(images/movies_bottom.png); width:100%; z-index:1;"></div>
</div>

<!-- Modal Window Code -->
<?php include('/includes/modalWindow.php'); ?>
</body>
</html>
